﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using SQLServerHealthCheck.Model;
using System.Windows.Media;
using AG.Utilities;

namespace SQLServerOptimizer.PL.WPF
{
    public partial class InstanceTemp : ResourceDictionary
    {

        public InstanceTemp()
        {
            InitializeComponent();
        }

        private void Instance_expander_Expanded(object sender, RoutedEventArgs e)
        {

            try
            {
                var expander = sender as Expander;

                System.Windows.Controls.TextBox InstanceTextBox = (System.Windows.Controls.TextBox)expander.FindName("txtInstance");
                if (InstanceTextBox == null) return;

                System.Threading.ThreadPool.QueueUserWorkItem(
                        (a) =>
                        {
                            System.Threading.Thread.Sleep(100);
                            InstanceTextBox.Dispatcher.Invoke(
                            new Action(() =>
                            {
                                InstanceTextBox.Focus();

                            }));
                        }
                        );

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private void UserPressedEnter(object sender, System.Windows.Input.KeyEventArgs e)
        {
            try
            {

                if (e.Key == System.Windows.Input.Key.Enter)
                {
                    SQLServerInstance SqlServerInstance = new SQLServerInstance();
                    SqlServerInstance.InstanceName = "<New Instance>";
                    SqlServerInstance.Port = "1433";
                    SqlServerInstance.Authentication = "Windows Authentication";
                    SqlServerInstance.Login = "";
                    SqlServerInstance.Password = "";

                    var mainWindow = (e.InputSource.RootVisual as MainWindow);

                    mainWindow.SQLServerInstances.Insert(mainWindow.SQLServerInstances.Count, SqlServerInstance);

                    var textbox = (sender as System.Windows.Controls.TextBox);

                    var oldExpander = ((textbox.Parent as Grid).Parent as Expander);

                    oldExpander.IsExpanded = false;

                    System.Threading.ThreadPool.QueueUserWorkItem(
                       (a) =>
                       {
                           System.Threading.Thread.Sleep(200);
                           mainWindow.Dispatcher.Invoke(
                           new Action(() =>
                           {
                               ListBoxItem ListBoxItem = (ListBoxItem)(mainWindow.Instance_ListBox.ItemContainerGenerator.ContainerFromItem(SqlServerInstance) as ListBoxItem);
                               if (ListBoxItem == null)
                                   return;


                               ContentPresenter ContentPresenter = FindVisualChild<ContentPresenter>(ListBoxItem);

                               DataTemplate InstanceDataTemplate = ContentPresenter.ContentTemplate;
                               Expander InstanceExpander = (Expander)InstanceDataTemplate.FindName("Instance_expander", ContentPresenter);

                               InstanceExpander.IsExpanded = true;
                           }));
                       }
                       );

                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
        private childItem FindVisualChild<childItem>(DependencyObject obj)
               where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        private void cboInstanceAuthentication_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            try
            {

                System.Windows.Controls.ComboBox cbo = (System.Windows.Controls.ComboBox)sender;

                System.Threading.ThreadPool.QueueUserWorkItem(
                        (a) =>
                        {
                            System.Threading.Thread.Sleep(100);
                            cbo.Dispatcher.Invoke(
                            new Action(() =>
                            {
                                try
                                {

                                    System.Windows.Controls.TextBox txtLogin = (System.Windows.Controls.TextBox)(cbo.Parent as Grid).FindName("txtInstanceLogin");
                                    System.Windows.Controls.Label lblLogin = (System.Windows.Controls.Label)(cbo.Parent as Grid).FindName("lblLogin");
                                    System.Windows.Controls.PasswordBox passwordBox = (System.Windows.Controls.PasswordBox)(cbo.Parent as Grid).FindName("passwordBox");
                                    System.Windows.Controls.Label lblPassword = (System.Windows.Controls.Label)(cbo.Parent as Grid).FindName("lblPassword");


                                    if (cbo.SelectedItem != null && cbo.SelectedItem.ToString().ToLower().Contains("windows"))
                                    {
                                        txtLogin.Visibility = Visibility.Hidden;
                                        lblLogin.Visibility = Visibility.Hidden;
                                        passwordBox.Visibility = Visibility.Hidden;
                                        lblPassword.Visibility = Visibility.Hidden;
                                    }
                                    else
                                    {
                                        if (txtLogin != null)
                                        {
                                            txtLogin.Visibility = Visibility.Visible;
                                            lblLogin.Visibility = Visibility.Visible;
                                            passwordBox.Visibility = Visibility.Visible;
                                            lblPassword.Visibility = Visibility.Visible;
                                        }

                                    }
                                }
                                catch (Exception ex)
                                {
                                    Logger.Error(ex);
                                }


                            }));
                        }
                        );

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }








        }

    }
}
